
package com.htj.erp.service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.htj.erp.dao.OrderbDao;
import com.htj.erp.dao.OrderhDao;
import com.htj.erp.dao.mybatis.OrderbMapper;
import com.htj.erp.exception.BusinessException;
import com.htj.erp.model.jpa.Orderb;
import com.htj.erp.model.jpa.Orderh;
import com.htj.erp.model.mybatis.OrderbPO;
import com.htj.erp.vo.OrderbVO;
import com.kedacom.security.service.GenericCpsService;
import com.kedacom.utils.BeanUtils;
/**
 * 
 *
 * 
 * OrderbService
 * orderb service 处理类
 * 这个基本类一般继承com.kedacom.security.service.GenericCpsService
 * 如果自己实现转换类，通过重写getXXConveter方法。
 * chenghongtao
 * 2016-11-07 15:30:51
 * HTJ
 * @version 1.0.0
 *
 */
@Service
public class OrderbService extends GenericCpsService<Orderb,OrderbPO,OrderbVO,Long>  {

    
	@Resource
	private OrderbDao orderbDao;
	@Resource
	private OrderhDao orderhDao;
	@Resource
	private OrderbMapper orderbMapper;
	
	/**
	 * 获取jpa的dao
	 */
	@Override
	protected  OrderbDao getEntityJpaDao ()
	{
		return this.orderbDao;
	}
	 
	  
	/**
	 * 获取 mubatis 的dao
	 */
	@Override
	protected  OrderbMapper getEntityDao ()
	{
		return this.orderbMapper;
	}
	
	
	
	/**
	 * 
	 * saveOrUpdate 保存或者更新
	 * @param orderbVO
	 * @return
	 *OrderbVO
	 * @exception
	 * @since  1.0.0
	 */
	public OrderbVO saveOrUpdate(OrderbVO orderbVO) throws Exception
	{
		
		orderbVO.setIsPlanSend(0);
	    if(orderbVO.getId() == null || orderbVO.getId() == 0L)
	    {
	        return this.add(orderbVO);
	    }
	    else
	    {
	        return this.update(orderbVO);
	    }
	    
	}
	public OrderbVO add (OrderbVO vo) throws Exception {
		Orderh orderh= this.orderhDao.findOne(vo.getOrderhId());
		if(orderh == null)
			throw new BusinessException("未找到更新对象");
		if(!orderh.getStatus().equals(Orderh.STATUS_INIT))
			throw new BusinessException("订单"+orderh.getCusName()+"已经提交确认，不能更新。");
		
		Orderb orderb = this.getMoConverter().convert(this.getMoClz(), vo);
		orderb.setOrderh(orderh);
		orderb.setTotalPrice(new BigDecimal(orderb.getProductNum()).multiply(new BigDecimal(orderb.getUnitPrice())).floatValue());
        return getVoConverter().convert(getVoClz(), save(orderb)); 
    }
	public OrderbVO update (OrderbVO vo) throws Exception {
		Orderb entity= this.getEntityJpaDao().findOne(vo.getId());
		if(entity == null)
			throw new BusinessException("未找到更新对象");
		if(!entity.getOrderh().getStatus().equals(Orderh.STATUS_INIT))
			throw new BusinessException("订单"+entity.getOrderh().getCusName()+"已经提交确认，不能更新。");
		
		BeanUtils.copySpecifiedProperties(entity, vo, "extOrderNo","productId","productName",
						"productSpec","unit","productNum","recvId","recvName","recvAdd","recvContactor","recvTel",
						"unitPrice");
		entity.setTotalPrice(new BigDecimal(entity.getProductNum()).multiply(new BigDecimal(entity.getUnitPrice())).floatValue());
        return getVoConverter().convert(getVoClz(), save(entity)); 
    }


	@Override
	public void delById(Long id) throws Exception {
		Orderb entity= this.getEntityJpaDao().findOne(id);
		if(entity == null)
			throw new BusinessException("未找到更新对象");
		if(!entity.getOrderh().getStatus().equals(Orderh.STATUS_INIT))
			throw new BusinessException("订单"+entity.getOrderh().getCusName()+"已经提交确认，不能删除。");
		super.delById(id);
	}


	@Override
	public void delByIds(Long[] ids) throws Exception {
		 for(Long id : ids)
			 this.delById(id);
	}
    
	
	public List<OrderbPO> listSendableOrderb (Map para)
	{
		 return this.getEntityDao().getSendableOrderb(para);
	}
}
